package org.jxkj.bean;


import org.jxkj.utils.DruidUtils;

import javax.validation.constraints.NotNull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CmPoint {
    static Connection connection = null;
    static ResultSet rs = null;
    static PreparedStatement pstmt = null;

    @NotNull(message = "测点编码不能为空")
    private String pointCode;

    @NotNull(message = "测点名称不能为空")
    private String pointName;

    @NotNull(message = "是否计算测点不能为空")
    private String calcFlag = "NORMAL";

    private String formula;


    public CmPoint(String pointCode, String pointName, String calcFlag, String formula) {
        this.pointCode = pointCode;
        this.pointName = pointName;
        this.calcFlag = calcFlag;
        this.formula = formula;
    }


    public CmPoint() {
    }


    public static ResultSet getRs() {
        return rs;
    }

    public void setPointCode(String pointCode) {
        this.pointCode = pointCode;
    }

    public String getPointCode() {
        return this.pointCode;
    }

    public void setPointName(String pointName) {
        this.pointName = pointName;
    }

    public String getPointName() {
        return this.pointName;
    }

    public void setCalcFlag(String calcFlag) {
        this.calcFlag = calcFlag;
    }

    public String getCalcFlag() {
        return this.calcFlag;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public String getFormula() {
        return this.formula;
    }


    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("CmPoint[");
        sb.append("pointCode=");
        sb.append(this.pointCode);
        sb.append(",pointName=");
        sb.append(this.pointName);
        sb.append(",calcFlag=");
        sb.append(this.calcFlag);
        sb.append(",formula=");
        sb.append(this.formula);
        sb.append("]");
        return sb.toString();
    }


    public static List<CmPoint> getCmPoint(String point) {
        List<CmPoint> list = new ArrayList<>();

        try {
            connection = DruidUtils.newInstance().getConnection();

            String sql = "select point_code,point_name,calc_flag,formula from hms_cm_point where point_code= ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, point);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String pointCode = rs.getString("point_code");
                String pointName = rs.getString("point_name");
                String calcFlag = rs.getString("calc_flag");
                String formula = rs.getString("formula");
                list.add(new CmPoint(pointCode, pointName, calcFlag, formula));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static List<CmPoint> queryWhere(String where) {
        List<CmPoint> list = new ArrayList<>();

        try {
            connection = DruidUtils.newInstance().getConnection();

            String sql = "select point_code,point_name,calc_flag,formula from hms_cm_point where " + where;

            pstmt = connection.prepareStatement(sql);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                String pointCode = rs.getString("point_code");
                String pointName = rs.getString("point_name");
                String calcFlag = rs.getString("calc_flag");
                String formula = rs.getString("formula");
                list.add(new CmPoint(pointCode, pointName, calcFlag, formula));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static Map<String, Double> queryToMap(String sql) throws SQLException {
        Map<String, Double> data = new HashMap<>();


        try {
            connection = DruidUtils.newInstance().getConnection();


            pstmt = connection.prepareStatement(sql);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                data.put(rs.getString("point_code"), Double.valueOf(rs.getDouble("calvalue")));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return data;
    }


    public static List<PointData> queryToRs(String sql) throws SQLException {
        ResultSet rs2 = null;
        Connection connection2 = null;
        List<PointData> list = new ArrayList<>();


        try {
            connection2 = DruidUtils.newInstance().getConnection();

            PreparedStatement pstmt2 = connection2.prepareStatement(sql);

            rs2 = pstmt2.executeQuery();
            while (rs2.next()) {
                list.add(new PointData(rs2.getString("point_code"), rs2.getString("stat_date"), rs2.getString("calvalue")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
